﻿using ctolua.Models.Enum;
using ctolua.Models.UI;
using ctolua.sys;
using ctolua.ZGame;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ctolua.Data.slk;

namespace ctolua.Models.Expand.EModel
{
    /// <summary>
    /// 冲锋
    /// </summary>
    public class charge
    {
        /// <summary>
        /// 冲锋终点
        /// </summary>
        public MPoint 终点 { get; set; }
        /// <summary>
        /// 冲锋距离
        /// </summary>
        public double 距离 { get; set; }
        /// <summary>
        /// 几秒冲锋结束 默认1秒
        /// </summary>
        public double 结束时间 { get; set; } = 1;
        public Action<MUnit, MUnit> 碰撞触发 { get; set; }
        public Action<MUnit> 冲锋结束 { get; set; }
        /// <summary>
        /// 默认为单位碰撞半径
        /// </summary>
        public double? 碰撞半径 { get; set; }
    }
    /// <summary>
    /// 击飞
    /// </summary>
    public class flay
    {
        public double 高度 { get; set; }
        /// <summary>
        /// 几秒击飞结束 默认1秒
        /// </summary>
        public double 结束时间 { get; set; } = 1;
        public Action<MUnit> 击飞结束 { get; set; }

    }

    public static class EMunit
    {
        /// <summary>
        /// 冲锋
        /// </summary>
        /// <param name="u">冲锋单位</param>
        /// <param name="c">冲锋参数</param>
        public static void 冲锋(this MUnit u, charge c)
        {
            if (c.碰撞半径 == null)
            {
                c.碰撞半径 = u.碰撞半径;
            }
            //计算到终点的角度
            double ang = Uitl.get_points_angle(u.pos.point, c.终点.point);
            //计算到终点的位置
            MPoint pos = u.pos.get_point_offset_vector(ang, c.距离);
            //单位碰撞半径
            double radius = u.unit.api_get_unit_collision_radius().@float();
            var tets = new Tween<MUnit>(c.结束时间, u, new List<Action<Tween<MUnit>>>() {
                        (data)=>{
                          u.x= data.getEasing( data.initObj._x,pos.x);
                          u.y= data.getEasing( data.initObj._y,pos.y);
                        },
                    }, Tween<MUnit>.outQuart);
            new MTimer(0, (Data) =>
            {
                try
                {
                    if (tets.update(0.03))
                    {
                        Data.del();
                        if (c.冲锋结束 != null)
                        {
                            c.冲锋结束.Invoke(u);
                        }
                    }
                    else
                    {
                        try
                        {
                            var unit = MUnitGroup.filter_unit_id_list_in_area_v2(u.pos.point, MShape.create_circular_shape(c.碰撞半径).shape, max_count: 999, exclude_unit: u.unit).pick();
                            foreach (var item in unit)
                            {
                                if (u.is_enemy(item))
                                {
                                    tets.setStop();
                                    if (c.碰撞触发 != null)
                                    {
                                        c.碰撞触发.Invoke(u, MUnit.obj(item));
                                    }
                                    break;
                                }

                            }

                        }
                        catch (Exception ex)
                        {
                            if (ZGame.Debug.is_debug)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                            
                        }

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            });
        }
        public static void 击飞(this MUnit u, flay c)
        {
            var tets = new Tween<MUnit>(c.结束时间, u, new List<Action<Tween<MUnit>>>() {
                        (data)=>{
                          u.z= data.getEasing( data.initObj._z,c.高度);
                        },
                    }, Tween<MUnit>.outQuart);
            new MTimer(0, (Data) =>
            {
                try
                {
                    if (tets.update(0.03))
                    {
                        Data.del();
                        if (c.击飞结束 != null)
                        {
                            c.击飞结束.Invoke(u);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            });
        }
        public static void 跳字(this MUnit u,object txt, EFloating floating= EFloating.系统文字)
        {
            Mui.create_harm_text(u.pos.point, (int)floating, Convert.ToString(txt) , MRoleGroup.get_all_role_ids.roleGroup);
        }
    }
}
